package com.icloud.article.boot.vo;

import io.github.doocs.im.model.group.GroupInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Collections;
import java.util.List;

/**
 * 方法说明: APP内所有群信息，含详情
 * 传入参数:
 * 返回的值:
 * 作者: java-101
 * 日期: 2024-05-14 13:46:13
 */
@ApiModel("APP内所有群信息，含详情")
public class IMGroupListVo<T> {

    @ApiModelProperty("群组列表（详情数据，接口文档：https://cloud.tencent.com/document/product/269/1616）")
    private List<T> data;

    @ApiModelProperty("上一页")
    private int lastPage;

    @ApiModelProperty("当前页")
    private int currentPage;

    @ApiModelProperty("下一页")
    private int nextPage;

    @ApiModelProperty("每页条数")
    private int pageSize;

    @ApiModelProperty("总页数")
    private int totalPage;

    @ApiModelProperty("总数据条数")
    private int totalCount;

    public IMGroupListVo(List<T> data, int currentPage, int pageSize) {
        if (data == null || data.isEmpty()) {
            throw new IllegalArgumentException("data must be not empty!");
        }

        this.data = data;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalCount = data.size();
        this.totalPage = (totalCount + pageSize - 1) / pageSize;
        this.lastPage = currentPage - 1 > 1 ? currentPage - 1 : 1;
        this.nextPage = currentPage >= totalPage ? totalPage : currentPage + 1;
    }
    public IMGroupListVo(int currentPage, int pageSize) {
        this.data = null;
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalCount = 0;
        this.totalPage = (totalCount + pageSize - 1) / pageSize;
        this.lastPage = currentPage - 1 > 1 ? currentPage - 1 : 1;
        this.nextPage = currentPage >= totalPage ? totalPage : currentPage + 1;
    }

    public int getPageSize() {
        return pageSize;
    }

    public List<T> getData() {
        int fromIndex = (currentPage - 1) * pageSize;
        if (fromIndex >= data.size()) {
            return Collections.emptyList();//空数组
        }
        if (fromIndex < 0) {
            return Collections.emptyList();//空数组
        }
        int toIndex = currentPage * pageSize;
        if (toIndex >= data.size()) {
            toIndex = data.size();
        }
        return data.subList(fromIndex, toIndex);
    }

    public int getLastPage() {
        return lastPage;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public int getTotalCount() {
        return totalCount;
    }

}
